/* The file is part of the Waff web framework.
 *
 * Copyright (c) 2011, Grzegorz Skorupa
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditionsare met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list of conditions
 *   and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *   and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * - Neither the name of the Grzegorz Skorupa nor the names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pl.org.waff.repository;

import java.util.Date;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Logger;

/**
 *
 * @author greg
 */
public class Document implements RepositoryObject {

    public static final String STATUS_TRASH = "trash";
    public static final String STATUS_PUBLISHED = "published";
    private String id = "new";
    private String language = "en";
    private String type = "";
    private String title = "";
    private String description = "";
    private String keywords = "";
    private String tags = "";
    private String taglist = "";
    private String author = "";
    private String createdby = "";
    private String version = "";
    private Date created;
    private Date modified;
    private Date published;
    private String content = "";
    private String folder = "";
    private String url = "";
    private String mimetype = "";
    private String size = "";
    private String width = "";
    private String height = "";
    private String status = "";
    private String target = "";
    private String nocomments = "";
    private static final Logger log = Logger.getLogger(Document.class.getName());

    public Document() {
        setCreated(new Date());
        setModified(getCreated());
    }

    public Document(Entity entity) {
        created = (Date) entity.getProperty("created");
        createdby = (String) entity.getProperty("createdby");
        modified = (Date) entity.getProperty("modified");
        published = (Date) entity.getProperty("published");
        id = KeyFactory.keyToString(entity.getKey());
        language = (String) entity.getProperty("language");
        type = (String) entity.getProperty("type");
        version = (String) entity.getProperty("version");
        title = (String) entity.getProperty("title");
        description = ((com.google.appengine.api.datastore.Text) entity.getProperty("description")).getValue();
        content = ((com.google.appengine.api.datastore.Text) entity.getProperty("content")).getValue();
        keywords = (String) entity.getProperty("keywords");
        tags = (String) entity.getProperty("tags");
        setTaglist((List) entity.getProperty("taglist"));
        author = (String) entity.getProperty("author");
        folder = (String) entity.getProperty("folder");
        url = (String) entity.getProperty("url");
        mimetype = (String) entity.getProperty("mimetype");
        size = (String) entity.getProperty("size");
        width = (String) entity.getProperty("width");
        height = (String) entity.getProperty("height");
        status = (String) entity.getProperty("status");
        target = (String) entity.getProperty("target");
        nocomments = (String) entity.getProperty("nocomments");
    }

    public Entity toEntity() {
        Entity entity;
        if ("new".equals(id) || "-1".equals(id)) {
            entity = new Entity("document");
        } else {
            entity = new Entity(KeyFactory.stringToKey(id));
        }
        entity.setProperty("created", created);
        entity.setProperty("createdby", createdby);
        entity.setProperty("modified", modified);
        entity.setProperty("published", published);
        entity.setProperty("language", language);
        entity.setProperty("type", type);
        entity.setProperty("version", version);
        entity.setProperty("title", title);
        entity.setProperty("content", new com.google.appengine.api.datastore.Text(content));
        entity.setProperty("description", new com.google.appengine.api.datastore.Text(description));
        entity.setProperty("keywords", keywords);
        entity.setProperty("tags", tags);
        entity.setProperty("taglist", getTaglistAsList());
        entity.setProperty("author", author);
        entity.setProperty("folder", folder);
        entity.setProperty("url", url);
        entity.setProperty("mimetype", mimetype);
        entity.setProperty("size", size);
        entity.setProperty("width", width);
        entity.setProperty("height", height);
        entity.setProperty("status", status);
        entity.setProperty("target", target);
        entity.setProperty("nocomments", nocomments);
        return entity;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the keywords
     */
    public String getKeywords() {
        return keywords;
    }

    /**
     * @param keywords the keywords to set
     */
    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    /**
     * @return the tags
     */
    public String getTags() {
        return tags;
    }

    /**
     * @param tags the tags to set
     */
    public void setTags(String tags) {
        this.tags = tags;
    }

    /**
     * @return the author
     */
    public String getAuthor() {
        return author;
    }

    /**
     * @param author the author to set
     */
    public void setAuthor(String author) {
        this.author = author;
    }

    /**
     * @return the version
     */
    public String getVersion() {
        return version;
    }

    /**
     * @param version the version to set
     */
    public void setVersion(String version) {
        this.version = version;
    }

    /**
     * @return the created
     */
    public Date getCreated() {
        return created;
    }

    /**
     * @param created the created to set
     */
    public void setCreated(Date created) {
        this.created = created;
    }

    public void setPublished(String published) {
        this.published = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            this.published = df.parse(published);
        } catch (ParseException e) {
            this.published = created;
        }
    }

    /**
     * @return the modified
     */
    public Date getModified() {
        return modified;
    }

    public Date getPublished() {
        if (published == null) {
            published = created;
        }
        return published;
    }

    /**
     * @param modified the modified to set
     */
    public void setModified(Date modified) {
        this.modified = modified;
    }

    public void setPublished(Date published) {
        this.modified = modified;
    }

    /**
     * @return the content
     */
    public String getContent() {
        return content;
    }

    /**
     * @param content the content to set
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * @return the language
     */
    public String getLanguage() {
        return language;
    }

    /**
     * @param language the language to set
     */
    public void setLanguage(String language) {
        this.language = language;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the folder
     */
    public String getFolder() {
        return folder;
    }

    /**
     * @param folder the folder to set
     */
    public void setFolder(String folder) {
        this.folder = folder;
    }

    public void save() {
        if ("new".equals(id) || "-1".equals(id)) {
            setCreated(new Date());
            setModified(getCreated());
            setPublished(getCreated());
        } else {
            setModified(new Date());
        }
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return the mimetype
     */
    public String getMimetype() {
        return mimetype;
    }

    /**
     * @param mimetype the mimetype to set
     */
    public void setMimetype(String mimetype) {
        this.mimetype = mimetype;
    }

    /**
     * @return the size
     */
    public String getSize() {
        return size;
    }

    /**
     * @param size the size to set
     */
    public void setSize(String size) {
        this.size = size;
    }

    /**
     * @return the width
     */
    public String getWidth() {
        return width;
    }

    /**
     * @param width the width to set
     */
    public void setWidth(String width) {
        this.width = width;
    }

    /**
     * @return the height
     */
    public String getHeight() {
        return height;
    }

    /**
     * @param height the height to set
     */
    public void setHeight(String height) {
        this.height = height;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @return the target
     */
    public String getTarget() {
        return target;
    }

    /**
     * @param target the target to set
     */
    public void setTarget(String target) {
        this.target = target;
    }

    public String getCreatedAsString(String dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(created);
    }

    public String getModifiedAsString(String dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(modified);
    }

    public String getPublishedAsString(String dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(getPublished());
    }

    /**
     * @return the createdby
     */
    public String getCreatedby() {
        return createdby;
    }

    /**
     * @param createdby the createdby to set
     */
    public void setCreatedby(String createdby) {
        this.createdby = createdby;
    }

    /**
     * @return the nocomments
     */
    public String getNocomments() {
        return nocomments;
    }

    /**
     * @param nocomments the nocomments to set
     */
    public void setNocomments(String nocomments) {
        this.nocomments = nocomments;
    }

    /**
     * @return the taglist
     */
    public String getTaglist() {
        return taglist;
    }

    public List getTaglistAsList() {
        List list = new ArrayList();
        StringTokenizer st = new StringTokenizer(taglist, ",");
        while (st.hasMoreElements()) {
            list.add(st.nextToken().trim());
        }
        return list;
    }

    /**
     * @param taglist the taglist to set
     */
    public void setTaglist(String taglist) {
        this.taglist = taglist;
    }

    public void setTaglist(List list) {
        StringBuffer result = new StringBuffer("");
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                result.append((String) list.get(i));
                if (i < list.size() - 1) {
                    result.append(", ");
                }
            }
        }
        taglist = result.toString();
    }
}
